package com.aceleeyy.sevenstar.controller;

import com.aceleeyy.sevenstar.config.ErrorMessage;
import com.aceleeyy.sevenstar.dao.MagazineMapper;
import com.aceleeyy.sevenstar.dao.MagazinePicturesMapper;
import com.aceleeyy.sevenstar.model.Magazine;
import com.aceleeyy.sevenstar.model.MagazinePictures;
import com.aceleeyy.sevenstar.model.Result;
import com.google.common.base.Throwables;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 杂志管理
 *
 * @auther: Ace Lee
 * @date: 2019/10/26 10:43
 */
@Api(value = "杂志管理",description = "杂志管理")
@Slf4j
@RestController
@RequestMapping("magazines")
public class MagazineController extends BaseController{

    @Autowired
    private MagazineMapper magazineMapper;
    @Autowired
    private MagazinePicturesMapper magazinePicturesMapper;

    @ApiOperation(value = "所有杂志")
    @GetMapping("")
    public Result all(HttpServletRequest request){
        Result result;
        log.info("所有杂志查询的请求开始");

        try {
            List<Magazine> rows = magazineMapper.selectAll();
            if (!CollectionUtils.isEmpty(rows)){
                log.info("所有杂志查询成功：{}",rows.size());
                rows.forEach(magazine -> {
                    List<MagazinePictures> pictures = magazinePicturesMapper.selectByMagazineId(magazine.getId());
                    magazine.setPictures(pictures);
                });
            }
            result = new Result<>(rows);
        } catch (Exception e) {
            log.error("所有杂志查询异常：{}", Throwables.getStackTraceAsString(e));
            result = Result.error(ErrorMessage.E_999.getErrDesc());
        }
        return result;
    }

    @ApiOperation(value = "年份查询")
    @GetMapping("/years")
    public Result allYears(HttpServletRequest request){
        Result result;
        log.info("年份查询的请求开始");

        try {
            List<String> rows = magazineMapper.selectAllYears();
            log.info("年份查询成功：{}",CollectionUtils.isEmpty(rows)?0:rows.size());
            result = new Result<>(rows);
        } catch (Exception e) {
            log.error("年份查询异常：{}", Throwables.getStackTraceAsString(e));
            result = Result.error(ErrorMessage.E_999.getErrDesc());
        }
        return result;
    }


    @ApiOperation(value = "年份杂志")
    @GetMapping("/year/{yyyy}")
    public Result allByYear(HttpServletRequest request, @PathVariable String yyyy){
        Result result;
        log.info("年份杂志查询的请求参数：{}",yyyy);
        if (StringUtils.isEmpty(yyyy)){
            return Result.error(ErrorMessage.E_001.getErrDesc());
        }

        try {
            String year = "%"+yyyy.trim()+"%";
            List<Magazine> rows = magazineMapper.selectAllByYear(year);
            if (!CollectionUtils.isEmpty(rows)){
                log.info("年份杂志查询成功：{}",rows.size());
                rows.forEach(magazine -> {
                    List<MagazinePictures> pictures = magazinePicturesMapper.selectByMagazineId(magazine.getId());
                    magazine.setPictures(pictures);
                });
            }
            result = new Result<>(rows);
        } catch (Exception e) {
            log.error("年份杂志查询异常：{}", Throwables.getStackTraceAsString(e));
            result = Result.error(ErrorMessage.E_999.getErrDesc());
        }
        return result;
    }


    @ApiOperation(value = "杂志详情")
    @GetMapping("/{id}")
    public Result get(HttpServletRequest request,@PathVariable Integer id){
        Result result;
        log.info("杂志详情的请求参数：{}",id);
        if (null==id){
            return Result.error(ErrorMessage.E_001.getErrDesc());
        }

        try {
            Magazine record = magazineMapper.selectByPrimaryKey(id);
            log.info("杂志详情查询成功：{}",record);
            if (null==record){
                result = Result.error(ErrorMessage.E_005.getErrDesc());
            }else {
                List<MagazinePictures> pictures = magazinePicturesMapper.selectByMagazineId(record.getId());
                record.setPictures(pictures);
                result = new Result<>(record);
            }
        } catch (Exception e) {
            log.error("杂志详情查询异常：{}", Throwables.getStackTraceAsString(e));
            result = Result.error(ErrorMessage.E_999.getErrDesc());
        }
        return result;
    }

}
